﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PBOffice.Models;

namespace PBOffice.DAL
{
    public class ManualSignService
    {
        public List<ManualSign> GetAllManualByDept(DateTime begin, DateTime end, string dept)
        {
            List<ManualSign> list = new List<ManualSign>();
            SqlParameter[] spa = { new SqlParameter("@BeginTime",begin),
                                    new SqlParameter("@EndTime",end),
                                    new SqlParameter("@Department",dept)
                                };
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, "proc_ManualStaffDept", spa))
            {
                while (dr.Read())
                {
                    Department dep = new Department();
                    dep.DeptName = dr["DeptName"].ToString();
                    dep.DeptId = (int)dr["DeptId"];
                    Staff sa = new Staff();
                    sa.Department = dep;
                    sa.StfId = (int)dr["StfId"];
                    sa.StfName = dr["StfName"].ToString();
                    sa.StfCode = dr["StfCode"].ToString();
                    ManualSign man = new ManualSign();
                    man.User = sa;
                    man.SignId = (int)dr["SignId"];
                    man.SignTime = Convert.ToDateTime(dr["SignTime"]);
                    man.SignTag = (int)dr["SignTag"];
                    man.SignDesc = dr["SignDesc"].ToString();
                    list.Add(man);
                }
            }
            return list;
        }
        public List<ManualSign> GetAllManualByNo(DateTime begin, DateTime end, string code)
        {
            List<ManualSign> list = new List<ManualSign>();
            SqlParameter[] spa = { new SqlParameter("@BeginTime",begin),
                                    new SqlParameter("@EndTime",end),
                                     new SqlParameter("@StfCode",code)
                                };
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, "proc_ManualStaffNo", spa))
            {
                while (dr.Read())
                {
                    Department dep = new Department();
                    dep.DeptName = dr["DeptName"].ToString();
                    dep.DeptId = (int)dr["DeptId"];
                    Staff sa = new Staff();
                    sa.Department = dep;
                    sa.StfId = (int)dr["StfId"];
                    sa.StfName = dr["StfName"].ToString();
                    sa.StfCode = dr["StfCode"].ToString();
                    ManualSign man = new ManualSign();
                    man.User = sa;
                    man.SignId = (int)dr["SignId"];
                    man.SignTime = Convert.ToDateTime(dr["SignTime"]);
                    man.SignTag = (int)dr["SignTag"];
                    man.SignDesc = dr["SignDesc"].ToString();
                    list.Add(man);
                }
            }
            return list;
        }
        public List<ManualSign> GetAllManualByUserId(DateTime begin, DateTime end, string name)
        {
            List<ManualSign> list = new List<ManualSign>();
            SqlParameter[] spa = { new SqlParameter("@BeginTime",begin),
                                    new SqlParameter("@EndTime",end),
                                    new SqlParameter("@UserId",name)
                                };
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, "proc_ManualStaffUserId", spa))
            {
                while (dr.Read())
                {
                    Department dep = new Department();
                    dep.DeptName = dr["DeptName"].ToString();
                    dep.DeptId = (int)dr["DeptId"];
                    Staff sa = new Staff();
                    sa.Department = dep;
                    sa.StfId = (int)dr["StfId"];
                    sa.StfName = dr["StfName"].ToString();
                    sa.StfCode = dr["StfCode"].ToString();
                    ManualSign man = new ManualSign();
                    man.User = sa;
                    man.SignId = (int)dr["SignId"];
                    man.SignTime = Convert.ToDateTime(dr["SignTime"]);
                    man.SignTag = (int)dr["SignTag"];
                    man.SignDesc = dr["SignDesc"].ToString();
                    list.Add(man);
                }
            }
            return list;
        }
        public void AddManualSign(ManualSign sign)
        {
            string sql = "insert [ManualSign] (UserId,SignTime,SignDesc,SignTag) values(@UserId,@SignTime,@SignDesc,@SignTag)";
            SqlParameter[] spa = {
                                     new SqlParameter("@UserId",sign.User.StfName),
                                     new SqlParameter("@SignTime",sign.SignTime),
                                     new SqlParameter("@SignDesc",sign.SignDesc),
                                     new SqlParameter("@SignTag",sign.SignTag)
                                 };
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql, spa);
        }
    }
}
